<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of sl2graph</title>
  <meta name="keywords" content="sl2graph">
  <meta name="description" content="sl2graph(g,p) -- create an SL(2,p) graph">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../../index.html">Home</a> &gt;  <a href="../index.html">matgraph</a> &gt; <a href="index.html">@graph</a> &gt; sl2graph.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../../index.html"><img alt="<" border="0" src="../../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for matgraph/@graph&nbsp;<img alt=">" border="0" src="../../right.png"></a></td></tr></table>-->

<h1>sl2graph
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>sl2graph(g,p) -- create an SL(2,p) graph</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>function sl2graph(g,p) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment"> sl2graph(g,p) -- create an SL(2,p) graph
 g is the graph to be created
 p is a prime
 the vertices of g correspond to 2-by-2 matrices with determinant equal to
 1 and entries modulo p
 Two vertices are adjacent if one can be obtained from the other by
 multiplication by either [1 1; 0 1] or [1 0; 1 1].
 In other words, g is a Cayley graph based on the group SL(2,p) with the
 two matrices as generators.</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="add.html" class="code" title="function add(g,i,j)">add</a>	add --- add edge(s) to the graph</li><li><a href="label.html" class="code" title="function label(g,v,name)">label</a>	Assign labels to vertices of g</li><li><a href="resize.html" class="code" title="function resize(g, n)">resize</a>	resize(g,n) --- change the number of vertices in g to n</li><li><a href="size.html" class="code" title="function [sz,tz] = size(g)">size</a>	size(g) --- returns [nv,ne] for the graph</li><li><a href="sparse.html" class="code" title="function sparse(g)">sparse</a>	sparse(g) --- convert internal storage for g to sparse</li></ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
</ul>
<!-- crossreference -->

<h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="#_sub1" class="code">function b = modinverse(a,p)</a></li><li><a href="#_sub2" class="code">function idx = findrow(row, A)</a></li></ul>
<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function sl2graph(g,p)</a>
0002 <span class="comment">% sl2graph(g,p) -- create an SL(2,p) graph</span>
0003 <span class="comment">% g is the graph to be created</span>
0004 <span class="comment">% p is a prime</span>
0005 <span class="comment">% the vertices of g correspond to 2-by-2 matrices with determinant equal to</span>
0006 <span class="comment">% 1 and entries modulo p</span>
0007 <span class="comment">% Two vertices are adjacent if one can be obtained from the other by</span>
0008 <span class="comment">% multiplication by either [1 1; 0 1] or [1 0; 1 1].</span>
0009 <span class="comment">% In other words, g is a Cayley graph based on the group SL(2,p) with the</span>
0010 <span class="comment">% two matrices as generators.</span>
0011 
0012 <span class="comment">% Make sure p is prime</span>
0013 
0014 <span class="keyword">if</span> ((p&lt;1) || (length(factor(p)) &gt; 1))
0015     error(<span class="string">'p must be prime'</span>)
0016 <span class="keyword">end</span>
0017 
0018 <span class="comment">% Generate a table of all matrices [ a b ; c d ] in SL2(p). There are p^3</span>
0019 <span class="comment">% of them that we store row-wise as [ a b c d ].</span>
0020 
0021 n = p^3-p;
0022 vtable = zeros(n,4);
0023 idx = 1;
0024 
0025 <span class="comment">% first the rows with a = 0</span>
0026 <span class="keyword">for</span> c=1:p-1
0027     b = p-<a href="#_sub1" class="code" title="subfunction b = modinverse(a,p)">modinverse</a>(c,p);
0028     <span class="keyword">for</span> d=0:p-1
0029         row = [ 0, b, c, d ];
0030         vtable(idx,:) = row;
0031         idx = idx+1;
0032     <span class="keyword">end</span>
0033 <span class="keyword">end</span>
0034 
0035 <span class="comment">% now the rows with a not equal to 0</span>
0036 <span class="keyword">for</span> a=1:p-1
0037     <span class="keyword">for</span> b=0:p-1
0038         <span class="keyword">for</span> c=0:p-1
0039             aa = <a href="#_sub1" class="code" title="subfunction b = modinverse(a,p)">modinverse</a>(a,p);
0040             d = aa*(b*c+1);
0041             d = mod(d,p);
0042             row = [ a, b, c, d ];
0043             vtable(idx,:) = row;
0044             idx = idx+1;
0045         <span class="keyword">end</span>
0046     <span class="keyword">end</span>
0047 <span class="keyword">end</span>
0048 
0049 
0050 <span class="comment">% DEBUG determinant check</span>
0051 <span class="keyword">for</span> idx=1:<a href="size.html" class="code" title="function [sz,tz] = size(g)">size</a>(vtable,1)
0052     row = vtable(idx,:);
0053     A = reshape(row,2,2);
0054     d = det(A);
0055     <span class="keyword">if</span> (mod(d,p) ~= 1)
0056         disp([row,d])
0057     <span class="keyword">end</span>
0058 <span class="keyword">end</span>
0059 
0060 vtable = sortrows(vtable);
0061 
0062 edgelist = [];
0063 <a href="resize.html" class="code" title="function resize(g, n)">resize</a>(g,0);
0064 <span class="keyword">if</span> (n&gt;1000)
0065     <a href="sparse.html" class="code" title="function sparse(g)">sparse</a>(g);
0066 <span class="keyword">end</span>
0067 <a href="resize.html" class="code" title="function resize(g, n)">resize</a>(g,n);
0068 
0069 <span class="keyword">for</span> v=1:n
0070     <a href="label.html" class="code" title="function label(g,v,name)">label</a>(g,v,int2str(reshape(vtable(v,:),2,2)));
0071 <span class="keyword">end</span>
0072 
0073 <span class="comment">% Caley graph generators</span>
0074 
0075 S = [ 1 1 ; 0 1 ];
0076 T = S';
0077 
0078 
0079 <span class="keyword">for</span> i = 1:n
0080     A = reshape(vtable(i,:),2,2);
0081     B = A*S;
0082     B = mod(B,p);
0083     j = <a href="#_sub2" class="code" title="subfunction idx = findrow(row, A)">findrow</a>(B(:)',vtable);
0084     edgelist = [edgelist; [i,j]];
0085     
0086     B = A*T;
0087     B = mod(B,p);
0088     j = <a href="#_sub2" class="code" title="subfunction idx = findrow(row, A)">findrow</a>(B(:)',vtable);
0089     edgelist = [edgelist; [i,j]];
0090 <span class="keyword">end</span>
0091 
0092 <a href="add.html" class="code" title="function add(g,i,j)">add</a>(g,edgelist);
0093 
0094 
0095 
0096 
0097 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
0098 
0099 <a name="_sub1" href="#_subfunctions" class="code">function b = modinverse(a,p)</a>
0100 <span class="comment">% computer inv(a) modulo p</span>
0101 [d,x,y] = gcd(a,p);
0102 b = mod(x,p);
0103 
0104 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
0105 
0106 <a name="_sub2" href="#_subfunctions" class="code">function idx = findrow(row, A)</a>
0107 <span class="comment">% find a row in a row sorted matrix, assume row is correct length</span>
0108 <span class="comment">% return 0 if not found</span>
0109 
0110 n = <a href="size.html" class="code" title="function [sz,tz] = size(g)">size</a>(A,1);
0111 idx = 0;
0112 <span class="keyword">if</span> n==0
0113     <span class="keyword">return</span>
0114 <span class="keyword">end</span>
0115 
0116 
0117 mid = ceil(n/2);
0118 midrow = A(mid,:);
0119 <span class="keyword">if</span> all(row == midrow)
0120     idx = mid;
0121     <span class="keyword">return</span>
0122 <span class="keyword">end</span>
0123 
0124 m = length(row); <span class="comment">% presumably 4</span>
0125 <span class="keyword">for</span> j=1:m
0126     <span class="keyword">if</span> row(j) &lt; midrow(j)
0127         idx = <a href="#_sub2" class="code" title="subfunction idx = findrow(row, A)">findrow</a>(row,A(1:mid-1,:));
0128         <span class="keyword">return</span>
0129     <span class="keyword">end</span>
0130     <span class="keyword">if</span> row(j) &gt; midrow(j)
0131         idx = <a href="#_sub2" class="code" title="subfunction idx = findrow(row, A)">findrow</a>(row,A(mid+1:<span class="keyword">end</span>,:));
0132         <span class="keyword">if</span> (idx&gt;0)
0133             idx = idx+mid;
0134         <span class="keyword">end</span>
0135         <span class="keyword">return</span>
0136     <span class="keyword">end</span>
0137 <span class="keyword">end</span>
0138 
0139 
0140 
0141 
0142 
0143</pre></div>
<hr><address>Generated on Fri 30-Apr-2010 07:51:16 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>